<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="6">
        <div class="form-item">
          <span class="title">储值卡类型</span>
          <el-select v-model="param.data.type" style="width:100%" placeholder="请选择">
            <el-option
              v-for="item in productOptions1"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="form-item">
          <span class="title">创建时间</span>
          <el-date-picker
            v-model="createdateList"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :clearable="false"
          ></el-date-picker>
        </div>
      </el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="8">
        <div class="form-item">
          <el-select v-model="type" style="width:40%" placeholder="请选择" @change="selectChange">
            <el-option
              v-for="item in productOptions"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
          <el-input
            placeholder="请输入储值卡名称"
            v-model="param.data.name"
            v-if="type =='1'"
            style="margin-left:10px"
          ></el-input>
          <el-input
            placeholder="请输入卡号表头"
            v-model="param.data.cardTitle"
            v-if="type =='2'"
            style="margin-left:10px"
          ></el-input>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="form-item button-container">
          <el-button type="primary" @click="search" icon="el-icon-search">查询</el-button>
          <el-button type="primary" @click="resetSearchCotnet" icon="el-icon-refresh">重置</el-button>
          <el-button
            type="primary"
            @click="addCard"
            v-if="isPermissionsBtn('coupon_add')"
            icon="el-icon-circle-plus-outline"
          >新增储值卡</el-button>
        </div>
      </el-col>
    </el-row>
    <div class="table-body">
      <el-table :data="tableData" border v-loading="isLoading" style="width: 100%">
        <el-table-column prop="name" label="名称" fixed></el-table-column>
        <el-table-column
          prop="type"
          label="类型"
          :formatter="(row) => { return row.type == '1' ? '赠送' : '企业' }"
        ></el-table-column>
        <el-table-column prop="money" label="面值">
          <template slot-scope="scope">
            ￥
            <span>{{scope.row.money}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column prop="creadStock" label="发放数量"></el-table-column>
        <el-table-column prop="startDate" label="有效期">
          <template slot-scope="scope">
            <span>{{scope.row.startDate}}</span>
            <p>至</p>
            <p>{{scope.row.endDate}}</p>
          </template>
        </el-table-column>
        <el-table-column prop="cardNO" label="卡号区间">
          <template slot-scope="scope">
            <span>{{scope.row.startNum}}</span>
            <p>至</p>
            <p>{{scope.row.endNum}}</p>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column fixed="right" label="操作" width="280">
          <template slot-scope="scope">
            <el-button
              @click="handleToDetail(scope.row)"
              v-if="isPermissionsBtn('card_detail')"
              size="small"
              type="success"
            >查看详情</el-button>
            <el-button
              @click="handleToCode(scope.row)"
              type="primary"
              size="small"
              v-if="isPermissionsBtn('card_code')"
            >导出二维码</el-button>
            <el-popconfirm title="确定禁用此卡？" @onConfirm="handleClickStop(scope.row)">
              <el-button
                style="margin-left:15px"
                size="small"
                type="danger"
                slot="reference"
                v-if="isPermissionsBtn('card_stop')"
              >禁用</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          @current-change="currentPageChange"
          layout="prev, pager, next"
          :total="total"
        ></el-pagination>
      </div>
    </div>
    <el-dialog title="新增储值卡" :visible.sync="isVisible" width="50%">
      <AddCard @submit="getMag" v-if="isVisible" />
    </el-dialog>
    <el-dialog title="储值卡详情" :visible.sync="isVisible1" width="50%">
      <CardDetail @submit="changeInfo" :data="propdata" v-if="isVisible1" />
    </el-dialog>
    <el-dialog :title="codeTitle" :visible.sync="isVisible2" width="50%" @close ="codeClose">
      <div id="qrcode" ref="qrCodeUrl" style="display: flex;flex-direction: row;align-items: center;justify-content: center;"></div>
      <div class="cs">请使用小程序扫码</div>
      <div style="text-align:center; margin-top: 50px;">
        <el-button type="primary" @click="updown()">下载图片</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import AddCard from "./AddCard";
import CardDetail from "./CardDetail";
import QRCode from "qrcodejs2";
export default {
  name: "",
  data() {
    return {
      codeTitle:"",
      propdata: {},
      isVisible: false,
      isVisible1: false,
      isVisible2: false,
      productOptions: [
        { id: "1", title: "储值卡名称" },
        { id: "2", title: "开号标头" }
      ],
      productOptions1: [
        { id: "1", title: "赠送" },
        { id: "2", title: "企业购买" }
      ],
      createdateList: [],
      param: {
        data: {
          type: "",
          name: "",
          startDate: "",
          endDate: "",
          cardTitle: ""
        },
        queryRequest: {
          page: 1,
          size: 10
        }
      },
      type: "1",
      isLoading: false,
      tableData: [],
      total: 1,
    };
  },
  components: {
    AddCard,
    CardDetail
  },
  computed: {
    ...mapGetters(["isPermissionsBtn"])
  },
  beforeMount() {},
  mounted() {
    this.initData();
  },
  methods: {
    updown(){
      //找到canvas标签
      const myCanvas = document.getElementById('qrcode').getElementsByTagName('canvas')
      //创建一个a标签节点
      const a = document.createElement('a')
      //设置a标签的href属性（将canvas变成png图片）
      a.href = myCanvas[0].toDataURL('image/png').replace('image/png', 'image/octet-stream')
      //设置下载文件的名字
      a.download = this.codeTitle+'二维码'+'.png'
      //点击
      a.click()
    },
    qrcode(id) {
      setTimeout(() => {
         var qrcode = new QRCode(this.$refs.qrCodeUrl, {
        text: id, // 需要转换为二维码的内容
        width: 250,
        height: 250,
        colorDark: "#000000",
        colorLight: "#ffffff",
        correctLevel: QRCode.CorrectLevel.H
      });
      }, 500);
    },
    selectChange(e) {
      if (e == "1") {
        this.param.data.cardTitle = "";
      } else {
        this.param.data.name = "";
      }
    },
    initData() {
      this.isLoading = true;
      if (this.createdateList.length > 0) {
        this.param.data.startDate = this.createdateList[0];
        this.param.data.endDate = this.createdateList[1];
      }
      this.$service.getStoredCardList(this.param).then(res => {
        this.isLoading = false;
        if (res.code == "00000") {
          if (res.data.rows.length == 0 && this.param.queryRequest.page != 1) {
            this.param.queryRequest.page -= 1;
            this.initData();
          }
          this.tableData = res.data.rows;
          this.total = res.data.total || 0;
        }
      });
    },
    search() {
      this.param.queryRequest.pageNum = 1;
      this.initData();
    },
    resetSearchCotnet() {
      //重置搜索条件
      this.param.data.type = "";
      this.param.data.name = "";
      this.param.data.startDate = "";
      this.param.data.endDate = "";
      this.param.data.cardTitle = "";
      this.type = "1";
      this.createdateList = [];
    },
    addCard() {
      this.isVisible = true;
    },
    handleToDetail(scope) {
      this.isVisible1 = true;
      this.propdata = JSON.parse(JSON.stringify(scope));
    },
    handleToCode(scope) {
      this.codeTitle =scope.name;
      this.isVisible2 = true;
      this.qrcode(scope.id);
    },
    codeClose(){
      this.$refs.qrCodeUrl.innerHTML ="";
    },
    handleClickStop(scope) {
      var param = {
        storedCardId: scope.id
      };
      this.$service.updateStoredCard(param).then(res => {
        if (res.code == "00000") {
          this.$message({
            message: "禁用成功",
            type: "success"
          });
          this.initData();
        } else {
          this.$message({
            message: res.message,
            type: "warning"
          });
        }
      });
    },
    currentPageChange() {},
    getMag() {
      this.isVisible = false;
      this.initData();
    },
    changeInfo() {
      this.isVisible1 = false;
      this.initData();
    }
  },
  watch: {}
};
</script>
<style scoped>
.cs{
  text-align: center;font-size: 15px;color: #333;margin-top: 10px;
}
</style>
